package com.example.testdb1.config;

import com.example.testdb1.interceptor.LoginInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@Slf4j
@EnableSwagger2
public class WebMvcConfiguration extends WebMvcConfigurationSupport {


    @Autowired
    private SwaggerConfig swaggerConfig;
    @Autowired
    private LoginInterceptor loginInterceptor;


    //拦截器配置
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor).addPathPatterns("/stu/*")
                .excludePathPatterns("/login", "/doc.html");// /**是拦截所有
    }

    @Bean
    public Docket docket() {
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("中州战力系统接口文档")
                .version("1.0")
                .description("中州最强战力接口文档")
                .build();

        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                // .groupName("管理端接口")
                .apiInfo(apiInfo)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.testdb1.controller"))
                // .apis(RequestHandlerSelectors.basePackage(swaggerConfig.getBasePackage()))
                .paths(PathSelectors.any())
                .build();

        return docket;
    }

    /**
     * 设置静态资源映射，否则接口文档无法显示
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");

        // registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        // registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    /**
     * 消息转化器
     *
     * @param converters
     */
    // protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
    //     MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
    //     converter.setObjectMapper(new JacksonObjectMapper());
    //     converter.add;
    // }




}
